Skip to content

gh-136914: Use inspect.isroutine() in DocTest's lineno computation #136930

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 25, 2025

Conversation

dlax
Copy link
Contributor

@dlax dlax commented Jul 21, 2025

Previously, DocTest's lineno of functools.cache()-decorated functions was not properly returned (None was returned) because the underlying computation, in DocTest._find_lineno(), relied on inspect.isfunction() which does not consider the decorated result as a function.

We now use the more generic inspect.isroutine(), as elsewhere in doctest's logic, thus fixing lineno computation for functools.cache()-decorated functions.

Previously, DocTest's lineno of `functools.cache()`-decorated functions
was not properly returned (None was returned) because the underlying
computation, in `DocTest._find_lineno()`, relied on
`inspect.isfunction()` which does not consider the decorated result as a
function.

We now use the more generic `inspect.isroutine()`, as elsewhere
in doctest's logic, thus fixing lineno computation for
functools.cache()-decorated functions.
@dlax dlax force-pushed the doctest-lineno-isroutine branch from b1e3185 to ddf3ab1 Compare July 21, 2025 13:59
@serhiy-storchaka serhiy-storchaka self-requested a review July 23, 2025 12:06
@serhiy-storchaka serhiy-storchaka added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels Jul 23, 2025
Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it also work for cached_property?

import functools


@functools.cache
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest using a custom class decorator here instead of functools.cache. We don't want this to depend on the implementation details of functools.cache, which could change in the future.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this may add unnecessary complexity to the tests. And such test will not guarantee that the code works also for functools.cached, which is a comon case. Other tests just test common cases.

@@ -0,0 +1 @@
Fix retrieval of :attr:`doctest.DocTest.lineno` for :func:`functools.cache`-decorated functions.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likewise, I'd suggest wording this more generally -- this affects any class decorator that uses functools.update_wrapper, not just functools.cache

@dlax
Copy link
Contributor Author

dlax commented Jul 23, 2025

Does it also work for cached_property?

Apparently not; should I make it work in the scope of this PR?

@serhiy-storchaka
Copy link
Member

If this is easy, why not? Otherwise this can be a separate PR.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. 👍

@dlax
Copy link
Contributor Author

dlax commented Jul 25, 2025

Added a commit to handle functools.cached_property.

@dlax dlax force-pushed the doctest-lineno-isroutine branch from 168503c to 553fb13 Compare July 25, 2025 07:17
@serhiy-storchaka serhiy-storchaka merged commit fece15d into python:main Jul 25, 2025
41 checks passed
@dlax dlax deleted the doctest-lineno-isroutine branch July 25, 2025 12:35
taegyunkim pushed a commit to taegyunkim/cpython that referenced this pull request Aug 4, 2025
…cTest's lineno computation (pythonGH-136930)

Previously, DocTest's lineno of functions and methods decorated with
functools.cache(), functools.lru_cache() and functools.cached_property()
was not properly returned (None was returned) because the
computation relied on inspect.isfunction() which does not consider the
decorated result as a function.

We now use the more generic inspect.isroutine(), as elsewhere
in doctest's logic.

Also, added a special case for functools.cached_property().
hugovk added a commit to hugovk/github-tools that referenced this pull request Aug 7, 2025
Fixes:

Checking branch 3.13
From https://github.com/python/cpython
 * [new branch]              3.13       -> 3.13

1 1 10000 python/cpython#136930
  gh-136914: Fix support of cached functions and properties in DocTest's lineno
computation (GH-136930)
    Backport to 3.13 missing
2 1 10000 python/cpython#136890
  Fix typos in Doc/extending/extending.rst and Doc/library/shelve.rst
(GH-136890)
    Backport to 3.13 missing
3 1 10000 python/cpython#136709
  gh-136710: Fix bad indentation in `os.chdir` docstring (GH-136709)
    Backport to 3.13 missing
4 1 10000 python/cpython#136519
  Omit `Python/perf_jit_trampoline.c` from the `**/*jit*` CODEOWNERS rule
(#136519)
fatal: command line, 'Omit `Python/perf_jit_trampoline.c` from the `**/*jit*` CODEOWNERS rule (#136519)': Invalid preceding regular expression
Traceback (most recent call last):
  File "/home/runner/work/dashboard/dashboard/github-tools/needs_backport.py", line 214, in <module>
    main()
    ~~~~^^
  File "/home/runner/work/dashboard/dashboard/github-tools/needs_backport.py", line 170, in main
    candidates = check_prs(
        api, args.repo_path, branch, args.start, args.number, args.sort
    )
  File "/home/runner/work/dashboard/dashboard/github-tools/needs_backport.py", line 109, in check_prs
    if is_commit_title_in_branch(repo_path, title, branch_to_check):
       ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/work/dashboard/dashboard/github-tools/needs_backport.py", line 60, in is_commit_title_in_branch
    output = subprocess.check_output(
             ~~~~~~~~~~~~~~~~~~~~~~~^
        ["git", "log", "--grep", title, branch], cwd=repo_path
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ).decode("utf-8")
    ^
  File "/opt/hostedtoolcache/Python/3.13.5/x64/lib/python3.13/subprocess.py", line 472, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
           ~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
               **kwargs).stdout
               ^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.13.5/x64/lib/python3.13/subprocess.py", line 577, in run
    raise CalledProcessError(retcode, process.args,
                             output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['git', 'log', '--grep', 'Omit `Python/perf_jit_trampoline.c` from the `**/*jit*` CODEOWNERS rule (#136519)', '3.13']' returned non-zero exit status 128.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants